πŸ‘©β€πŸ’» 17679. ν”„λ Œμ¦ˆ4블둝

@Seongukbaek Posted May 22, 2022 Β· Updated May 22, 2022 Β· 3 min read

문제

Programmers - ν”„λ Œμ¦ˆ4블둝

πŸ“ Logic

while (f.checkBlocks()) { answer += f.countBlocks(); f.dropBlocks(); }
  • 흐름은,

    • λ³΄λ“œμ— 4블둝이 μžˆλŠ”μ§€ ν™•μΈν•˜κ³ ,
    • μžˆλ‹€λ©΄ 없앨 수 μžˆλŠ” 블둝 수λ₯Ό κ³„μ‚°ν•˜κ³ ,
    • λ³΄λ“œλ₯Ό μ΅œμ‹ ν™”ν•œλ‹€.
public boolean checkBlocks() { check = new boolean[m][n]; boolean has = false; for (int i = 0; i < m - 1; i++) for (int j = 0; j < n - 1; j++) if (blocks[i][j] != '-' && has4Blocks(i,j)) { check[i][j] = true; check[i][j + 1] = true; check[i + 1][j] = true; check[i + 1][j + 1] = true; has = true; } return has; }
  • μ‚¬λΌμ§ˆ 블둝이 μ•„λ‹ˆκ³ , 4개의 λΈ”λ‘μŒμ„ 이룬닀면, ν•΄λ‹Ή μ’Œν‘œλ“€μ˜ check λ₯Ό true 둜 λ°”κΏ” 후에 μ‚¬λΌμ§ˆ κ²ƒμž„μ„ μ €μž₯ν•œλ‹€.

πŸ“„ CODE

μ½”λ“œ 보기/μ ‘κΈ°πŸ’«
import java.util.*; class Friends { char[][] blocks; boolean[][] check; int m; int n; public Friends(int m, int n, String[] board) { this.m = m; this.n = n; blocks = new char[m][n]; initBlocks(board); } private void initBlocks(String[] board) { for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) blocks[i][j] = board[i].charAt(j); } public boolean checkBlocks() { check = new boolean[m][n]; boolean has = false; for (int i = 0; i < m - 1; i++) for (int j = 0; j < n - 1; j++) if (blocks[i][j] != '-' && has4Blocks(i,j)) { check[i][j] = true; check[i][j + 1] = true; check[i + 1][j] = true; check[i + 1][j + 1] = true; has = true; } return has; } private boolean has4Blocks(int x, int y) { return blocks[x][y] == blocks[x][y + 1] && blocks[x][y] == blocks[x + 1][y] && blocks[x][y] == blocks[x + 1][y + 1]; } public int countBlocks() { int count = 0; for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) { if (!check[i][j]) continue; count++; blocks[i][j] = '-'; } return count; } public void dropBlocks() { for (int i = m - 1; i >= 0; i--) for (int j = 0; j < n; j++) if (blocks[i][j] == '-') pullBlocks(i, j); } private void pullBlocks(int x, int y) { for (int k = x - 1; k >= 0; k--) { if (blocks[k][y] != '-') { blocks[x][y] = blocks[k][y]; blocks[k][y] = '-'; break; } } } } class Solution { public int solution(int m, int n, String[] board) { Friends f = new Friends(m, n, board); int answer = 0; while (f.checkBlocks()) { answer += f.countBlocks(); f.dropBlocks(); } return answer; } }

✏️ Review

  • μ‹œν‚€λŠ” λŒ€λ‘œ ν•˜λ©΄ λ˜λŠ” λ¬Έμ œμ˜€λ‹€.
@Seongukbaek
배우고 μ •λ¦¬ν•˜κ³  κ³΅μœ ν•˜λŠ” λŒ€ν•™μƒ λ°±μ„±μš±μž…λ‹ˆλ‹€ πŸ˜€